package cn.yunyichina.provider.message.main;

import cn.yunyichina.provider.framework.dubbo.service.impl.YytServiceImpl;
import cn.yunyichina.provider.iface.entity.common.ResponseObject;
import cn.yunyichina.provider.iface.entity.message.AppMessageVo;
import cn.yunyichina.provider.iface.service.IMessage;
import cn.yunyichina.provider.message.service.impl.CardMessageServiceImpl;
import cn.yunyichina.provider.message.service.impl.MsgAppServiceImpl;
import cn.yunyichina.provider.message.service.impl.MsgServiceImpl;
import cn.yunyichina.provider.message.service.impl.MsgSmsServiceImpl;
import cn.yunyichina.utils.convert.JsonUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;

/**
 * 消息推送服务子系统dubbo入口
 *
 * @author zhangkw
 * @date 2016-07-29
 */
public class MessageServiceImpl extends YytServiceImpl implements IMessage {

    @Resource
    MsgSmsServiceImpl msgSmsService;

    @Resource
    MsgServiceImpl msgService;

    @Resource
    MsgAppServiceImpl msgAppService;

    private static Logger logger = LoggerFactory.getLogger(MessageServiceImpl.class);

    @Resource
    CardMessageServiceImpl cardMessageService;

    public String getErrorCode() {
        return "8000";
    }

    public ResponseObject sendSms(String strJson) {
        logger.info("发送短信MessageServiceImpl->sendSms请求:" + strJson);

        JSONObject param = JsonUtils.parseObject(strJson);
        param.put("msg_type", param.getInteger("sms_type"));
        param.put("to_user", param.getString("sms_mobile"));

        JSONObject msgContent = JsonUtils.parseObject(strJson);
        msgContent.remove("sms_type");
        msgContent.remove("sms_mobile");

        param.put("msg_content", msgContent);
        ResponseObject resObj = msgSmsService.sendMsgSms(JsonUtils.toJsonString(param));

        logger.info("发送短信MessageServiceImpl->sendSms返回:" + JSON.toJSONString(resObj));

        return resObj;
    }

    public String sendMsgRegNotify(String strJson) {
        return msgService.sendMsgRegNotify(strJson);
    }

    public ResponseObject sendCardMessage(String json) {
        return cardMessageService.sendCardMessage(json);
    }


    /**
     * 消息系统重构内部调用
     * @param appMessageVo
     * @return
     */
    public ResponseObject sendAppMessage(AppMessageVo appMessageVo) {
        logger.info("发送指定类型应用消息MessageServiceImpl->sendAppMessage请求:" + JsonUtils.toJsonString(appMessageVo));

        ResponseObject resObj = msgAppService.sendMsgAppGod(appMessageVo);

        logger.info("发送指定类型应用消息MessageServiceImpl->sendAppMessage返回:" + JSON.toJSONString(resObj));

        return resObj;
    }

}
